package com.chenyongjing.design.pattern.chain.auth.builderchain;

import com.chenyongjing.design.pattern.chain.auth.Member;

/**
 * @Author chen
 * @Date 2022/11/17 22:22
 */
public abstract class Handler<T> {
    protected Handler next;

    /**
     * 指向下一个处理节点
     * @param next
     */
    public void nextHandler(Handler next){
       this.next =next;
    }

    /**
     * 具体节点处理方法
     * @param member
     */
    public abstract void doHandler(Member member);

    /**
     * 通过建造者模式实现链式编程
     * @param <T>
     */
    public static class Builder<T>{
        private Handler<T> head;
        private Handler<T> tail;

        /**
         * 添加handler提娜姬方法（类似单项链表）
         * @param handler
         * @return
         */
        public  Builder<T> addHandler(Handler<T> handler){
            if(this.head == null){
                this.head = this.tail =handler;
                return this;
            }
            this.tail.nextHandler(handler);
            this.tail=handler;
            return this;
        }



        public Handler<T> build(){
            return this.head;
        }
    }


}
